package cn.afterturn.easypoi.word;

import cn.afterturn.easypoi.word.entity.bookmark.ExportExecel.NormalParagraph;
import cn.afterturn.easypoi.word.entity.bookmark.ExportExecel.TablePara;
import cn.afterturn.easypoi.word.entity.bookmark.WordEntityVO;
import cn.afterturn.easypoi.word.entity.bookmark.WordEntityPrint;
import cn.afterturn.easypoi.word.parse.BookmarkHandle;
import cn.afterturn.easypoi.word.parse.BookmarkHandleFromWordEntityToMap;
import cn.afterturn.easypoi.word.parse.excel.ExcelToListUtil;
import cn.afterturn.easypoi.word.vo.WordPage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;

import java.io.*;
import java.util.*;

import static org.junit.Assert.assertTrue;

public class wordTestListExtH {

    @Test
    public  void getInputStreamwordParseGetBookMark() throws Exception{
        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";
        String url=relativefilePath+"t清单文件Normal.docx";
        InputStream inputStream = new FileInputStream(new File(url));
        WordEntityVO wordEntityVO = cn.afterturn.easypoi.word.WordExportUtilExtH.getWord07BookMark(
                inputStream,url);
        WordPage wordPage= WordEntityPrint.exportToWordPage(wordEntityVO);
        System.out.println(wordPage.getNormalParagraphList().size()+"---");
        System.out.println(wordPage.getTableParaList().size());
     //   assertTrue(wordPage.getNormalParagraphList().size() ==14);
     //   assertTrue(wordPage.getTableParaList().size() ==13);
    }
    /*
    * 把提取参数的输入名称，输出名称统一作为参数
    * */

    @Test
    public  void getwordParseGetBookMarkToMap() throws Exception{
        String importFilePath="";
        String verifyfile="";
        String outputFileExcelPath="";
        String outputwordFile="";

        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";

        importFilePath=relativefilePath+"t清单文件Normal.docx";
        verifyfile=relativefilePath+"t清单文件NormalVerify.xlsx";
        outputFileExcelPath=relativefilePath+"t清单文件NormalAftertResult.xlsx";
          //  String fileName=relativefilePath+"t清单文件Normal.docx";
            System.out.println("当前处理的文件名为："+importFilePath+"\r\n");
            WordEntityVO wordEntityVO;
            wordEntityVO = cn.afterturn.easypoi.word.WordExportUtilExtH.getWord07BookMark(
                    importFilePath);
            //插入Map的新方法
            BookmarkHandleFromWordEntityToMap bookmarkHandleFromWordEntityToMap=new BookmarkHandleFromWordEntityToMap();
            Map<String,Object> mnew=bookmarkHandleFromWordEntityToMap.getMap(wordEntityVO);
            assertTrue(mnew.size()>10);
    }

    @Test
    public  void getwordParseGetBookMark(){
        String importFilePath="";
        String verifyfile="";
        String outputFileExcelPath="";
        String outputwordFile="";

        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";

        importFilePath=relativefilePath+"t清单文件Normal.docx";
        verifyfile=relativefilePath+"t清单文件NormalVerify.xlsx";
        outputFileExcelPath=relativefilePath+"t清单文件NormalAftertResult.xlsx";

        getwordParseBookMark(importFilePath,verifyfile,outputFileExcelPath);

    }

    private   void getwordParseBookMark(String fileName,String exportVerfyFilepath,String exportImportFilepath ){
        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";
        //把数据转为Map
        try {
            //  String fileName=relativefilePath+"t清单文件Normal.docx";
            System.out.println("当前处理的文件名为："+fileName+"\r\n");
            WordEntityVO wordEntityVO;
            wordEntityVO = cn.afterturn.easypoi.word.WordExportUtilExtH.getWord07BookMark(
                    fileName);


            WordPage wordPage= WordEntityPrint.exportToWordPage(wordEntityVO);
            //String verifypath=relativefilePath+"Verifyt清单文件Normal.xlsx";
            //比较奇怪的是在输出的时候，对wordPage中的集合有导出的时候，对原始的集合内的数据进行del了，所以需要COpy一份给
            //后面继续需要输出的方法
            WordPage wordPage1=WordEntityPrint.exportFile(wordPage,exportVerfyFilepath);
            wordPage1=WordEntityPrint.handWordPageStandard(wordPage1);
            //String resultPath=relativefilePath+"result清单文件Normal.xlsx";
            WordEntityPrint.exportFile(wordPage1,exportImportFilepath);
           // WordEntityPrint.exportToExcelEntity(wordEntityVO,exportVerfyFilepath,exportImportFilepath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /*
    2020-2-16已经测试
    高亮显示标签
    如果采用把textbox删掉，就可以正常识别{{}}中的内容
    在testtable.docx中，有Textbox，无法识别textbox里面的内容，暂时还不知道如何处理
    另外，针对高亮的显示，有需要针对{{}}这里面的内容进行高亮。但是暂时还没有调整
    * */
    @Test
    public void HightLightimportExcelToParseWord07Bookmark() {
        System.out.println("进入高亮标签");
        //导入Excel
        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";
       // String exportImportFilepath=relativefilePath+"deleteTextboxtesttable.docx";
        String exportImportFilepath=relativefilePath+"t清单文件Normal.docx";
        try {
            WordEntityVO wordEntityVO =new WordEntityVO();
            String highlightcolor="ffff00";
            String normalcolor="ffffff";
            XWPFDocument doc = WordExportUtilExtH
                    .HighLightexportWord07BookMark(exportImportFilepath,highlightcolor);
            //FileOutputStream fos = new FileOutputStream(relativefilePath+"HighlightResult测试结算审核报告 (中衡模板).docx");
            FileOutputStream fos = new FileOutputStream(relativefilePath+"HighlightResultt清单文件Normal.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /*
    2020-2-13已经测试通过
    遗留颜色控制的问题，没有在原来的Para中进行控制颜色
    Now的时间没有取到
    * */
    @Test
    public void importExcelToParseWord07Bookmark() {
        System.out.println("print");
        //导入Excel
        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";
        String exportImportFilepath=relativefilePath+"sjkimportces2.xlsx";
        String SaveUrl=relativefilePath+"exportimage";
        List<NormalParagraph> resultNormal=ExcelToListUtil.readExcelToList(exportImportFilepath,SaveUrl);
        //assertTrue(resultNormal.size() ==12);
        resultNormal.forEach(the -> {
            System.out.println(the.toString());
        });

        System.out.println("-----********************************--------------------------");
        List<TablePara> resulttable=ExcelToListUtil.readExcelToListTablePara(exportImportFilepath,SaveUrl);
        assertTrue(resulttable.size() ==19);
        resulttable.forEach(t -> {
            System.out.println(t.toString());
        });


        //从Excel获取到类中，然后根据类进行
        //第一步过滤及处理。
        //然后把它写入到WordEntity中。
        //然后根据WordEntity处理到Map中。
        //然后调用服务，就可以得到生成的文件
        BookmarkHandle bookmarkHandle=new BookmarkHandle();
        bookmarkHandle.isFromNetWork=false;
        Map<String, Object> mapNormal =bookmarkHandle.handleNormalParagraph(resultNormal);
        Map<String, Object> mapTable = bookmarkHandle.handleTableParagraphExt(resulttable);
        Map<String,Object>  mapcombined=bookmarkHandle.mapcombined(mapNormal,mapTable);
              try {
            WordEntityVO wordEntityVO =new WordEntityVO();
            String highlightcolor="ffff00";
            String normalcolor="ffffff";
            XWPFDocument doc = WordExportUtilExtH
                    .exportWord07BookMark(relativefilePath+"t清单文件Normal.docx", mapcombined, wordEntityVO,highlightcolor);
                  /*XWPFDocument doc = WordExportUtilExtH
                          .exportWord07BookMarkExt(relativefilePath+"t清单文件Normal.docx", mapcombined,highlightcolor);*/
            FileOutputStream fos = new FileOutputStream(relativefilePath+"t清单文件NormalResult.docx");
            doc.write(fos);
            fos.close();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    @Test
    public  void wordParseMutltiGetBookMark() {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", "小明" + i);
            list.add(map);
        }
        String filePath=System.getProperty("user.dir");
        String relativefilePath=filePath+"\\src\\main\\java\\cn\\afterturn\\easypoi\\word\\files\\";
        String exportImportFilepath=relativefilePath+"loan.docx";

        //----------------------------------------------
        try {
            String highlightcolor="ffff00";
            String normalcolor="ffffff";
            XWPFDocument doc = WordExportUtilExtH
                    .exportWord07(exportImportFilepath, list,highlightcolor);

            FileOutputStream fos = new FileOutputStream(relativefilePath+"ManyPageWordTest.拼接多页测试.docx");
            doc.write(fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
